/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.insurance.cis.views;

import com.insurance.cis.connection.CisConnection;
import com.insurance.cis.controllers.ProductController;
import com.insurance.cis.utils.MessageDialogUtils;
import java.awt.Color;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Thuy Truc
 */
public class ProductJPanelView extends javax.swing.JPanel {

    /**
     * Creates new form ProductJPanelView
     */
    public ProductJPanelView(javax.swing.JFrame jFrame) {
        initComponents();
        loadDataToTable();
        loadCategories();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGroupSearch = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        txaDescription = new javax.swing.JTextArea();
        txtProductID = new javax.swing.JTextField();
        txtProductName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        rbtnProductName = new javax.swing.JRadioButton();
        btnSearch = new javax.swing.JButton();
        cboCategories = new javax.swing.JComboBox();
        ScrollProductList = new javax.swing.JScrollPane();
        tblProductList = new javax.swing.JTable();
        rbtnProductID = new javax.swing.JRadioButton();
        btnReset = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();
        btnInsert = new javax.swing.JButton();
        txtSearch = new javax.swing.JTextField();
        btnRefresh = new javax.swing.JButton();
        rbtnProductAll = new javax.swing.JRadioButton();

        txaDescription.setColumns(20);
        txaDescription.setRows(5);
        jScrollPane1.setViewportView(txaDescription);

        jLabel3.setText("Description");

        jLabel4.setText("Categories");

        jLabel1.setText("Product ID");

        jLabel2.setText("Product Name");

        btnGroupSearch.add(rbtnProductName);
        rbtnProductName.setText("ProductName");

        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        cboCategories.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Select Category--" }));

        ScrollProductList.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Product List"));

        tblProductList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        ScrollProductList.setViewportView(tblProductList);

        btnGroupSearch.add(rbtnProductID);
        rbtnProductID.setText("ProductID");

        btnReset.setText("Reset");

        btnDelete.setText("Delete");

        btnUpdate.setText("Update");

        btnInsert.setText("Insert");
        btnInsert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInsertActionPerformed(evt);
            }
        });

        txtSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSearchActionPerformed(evt);
            }
        });

        btnRefresh.setText("Refresh");

        btnGroupSearch.add(rbtnProductAll);
        rbtnProductAll.setText("All");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ScrollProductList, javax.swing.GroupLayout.DEFAULT_SIZE, 725, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(81, 81, 81)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING))
                        .addGap(37, 37, 37)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtProductID)
                            .addComponent(txtProductName)
                            .addComponent(jScrollPane1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cboCategories, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(102, 102, 102))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnInsert)
                        .addGap(18, 18, 18)
                        .addComponent(btnUpdate)
                        .addGap(18, 18, 18)
                        .addComponent(btnDelete)
                        .addGap(18, 18, 18)
                        .addComponent(btnReset)
                        .addGap(18, 18, 18)
                        .addComponent(btnRefresh)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addComponent(rbtnProductID)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbtnProductName)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbtnProductAll)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31)
                .addComponent(btnSearch)
                .addGap(43, 43, 43))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rbtnProductID)
                    .addComponent(rbtnProductName)
                    .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSearch)
                    .addComponent(rbtnProductAll))
                .addGap(18, 18, 18)
                .addComponent(ScrollProductList, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtProductID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtProductName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(cboCategories, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnInsert)
                    .addComponent(btnUpdate)
                    .addComponent(btnDelete)
                    .addComponent(btnReset)
                    .addComponent(btnRefresh))
                .addContainerGap(38, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        if(!rbtnProductID.isSelected() && !rbtnProductName.isSelected()){
            MessageDialogUtils.dialogError(this, "Please choose search mode!");
            return;
        }else{
            FindProduct();
        }
    }//GEN-LAST:event_btnSearchActionPerformed
    private void loadDataToTable()
    {
        Vector tit=new Vector();
        tit.add("Product ID");
        tit.add("Product Name");
        tit.add("Description");
        tit.add("Category");
        Vector vt=ProductController.readProduct();
        tblProductList.setModel(new DefaultTableModel(vt, tit));
        tblProductList.setDragEnabled(false);
    }
    private void loadCategories(){
        try {
            String sql = "Select ProductCatDesc from ProductCategory";
            Statement st = CisConnection.getInstance().getSqlConnection().createStatement();
            ResultSet rs = st.executeQuery(sql);
            while(rs.next()){
                cboCategories.addItem(rs.getString("ProductCatDesc"));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
       
    }
    private void FindProduct() {
        int ByID = 1;
        int ByName = 2;
        if (txtSearch.getText().equals("")) {
            if(!rbtnProductAll.isSelected()){
                txtSearch.setBackground(new Color(255, 153, 153));
                txtSearch.requestFocus();
                MessageDialogUtils.dialogError(this, "Please enter product to search!");
                return;
            }
        }
        if (rbtnProductID.isSelected()) {
            txtSearch.setBackground(new Color(255, 255, 255));
            Vector vt = ProductController.SearchProduct(txtSearch.getText(), ByID);
            Vector tit = new Vector();
            tit.add("Product ID");
            tit.add("Product Name");
            tit.add("Description");
            tit.add("Category");
            tblProductList.setModel(new DefaultTableModel(vt, tit));
            tblProductList.setDragEnabled(false);
            if(vt.isEmpty()){
                MessageDialogUtils.dialogMessage(this, "No product found!");
            }
        } else if (rbtnProductName.isSelected()){
            txtSearch.setBackground(new Color(255, 255, 255));
            Vector vt = ProductController.SearchProduct(txtSearch.getText(), ByName);
            Vector tit = new Vector();
            tit.add("Product ID");
            tit.add("Product Name");
            tit.add("Description");
            tit.add("Category");
            tblProductList.setModel(new DefaultTableModel(vt, tit));
            tblProductList.setDragEnabled(false);
            if(vt.isEmpty()){
                MessageDialogUtils.dialogMessage(this, "No product found!");
            }
        }else{
            txtSearch.setText("");
            txtSearch.setBackground(new Color(255, 255, 255));
            loadDataToTable();
        }
    }
    
    private void btnInsertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInsertActionPerformed
        
    }//GEN-LAST:event_btnInsertActionPerformed

    private void txtSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtSearchActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtSearchActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane ScrollProductList;
    private javax.swing.JButton btnDelete;
    private javax.swing.ButtonGroup btnGroupSearch;
    private javax.swing.JButton btnInsert;
    private javax.swing.JButton btnRefresh;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnSearch;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JComboBox cboCategories;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JRadioButton rbtnProductAll;
    private javax.swing.JRadioButton rbtnProductID;
    private javax.swing.JRadioButton rbtnProductName;
    private javax.swing.JTable tblProductList;
    private javax.swing.JTextArea txaDescription;
    private javax.swing.JTextField txtProductID;
    private javax.swing.JTextField txtProductName;
    private javax.swing.JTextField txtSearch;
    // End of variables declaration//GEN-END:variables
}
